<!-- 班级编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改班级':'添加班级'"
    :visible="visible"
    width="550px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px">

      <el-form-item
        label="编号:"
        prop="code">
        <el-input
          :maxlength="20"
          v-model="form.code"
          placeholder="请输入编号"
          clearable/>
      </el-form-item>

      <el-form-item
        label="班级名称:"
        prop="name">
        <el-input
          :maxlength="20"
          v-model="form.name"
          placeholder="请输入班级名称"
          clearable/>
      </el-form-item>

      <el-form-item label="所属组织:" prop="organization">
          <el-select
            filterable
            clearable
            v-model="form.organization"
            @change="getDepartmentsList"
            size="small"
            placeholder="-请选择所属组织-"
            class="ele-block">
            <el-option v-for="item in schoolList" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
      </el-form-item>

      <el-form-item label="年级:" prop="grade">
          <el-select
            filterable
            clearable
            v-model="form.grade"
            @change="$forceUpdate()"
            size="small"
            placeholder="-请选择年级-"
            class="ele-block">
            <el-option v-for="item in gradeList" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
      </el-form-item>

      <el-form-item label="院系类型:" prop="stype">
          <el-select
            clearable
            v-model="form.stype"
            @change="getDepartmentsList"
            size="small"
            placeholder="-请选择院系类型-"
            class="ele-block">
            <el-option label="自然院系" :value="1"/>
            <el-option label="体育院系" :value="2"/>
          </el-select>
      </el-form-item>

      <el-form-item label="所属院系:" prop="superior">
          <el-select
            filterable
            clearable
            v-model="form.superior"
            @change="getProfessionalList"
            size="small"
            placeholder="-请选择所属院系-"
            class="ele-block">
            <el-option v-for="item in departmentsList" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
      </el-form-item>


      <el-form-item label="专业:" prop="professional">
          <el-select
            clearable
            v-model="form.professional"
            @change="$forceUpdate()"
            size="small"
            placeholder="-请选择专业-"
            class="ele-block">
            <el-option v-for="item in professionalList" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
      </el-form-item>
      <el-form-item label="状态:">
        <el-radio-group
          v-model="form.status">
                  <el-radio :label="1">正常</el-radio>
                  <el-radio :label="2">停用</el-radio>
                </el-radio-group>
      </el-form-item>

      <el-form-item
        label="负责人:"
        prop="head">
        <el-select
          clearable
          v-model="form.head"
          @change="$forceUpdate()"
          size="small"
          placeholder="-请选择负责人-"
          class="ele-block">
          <el-option v-for="item in headList" :key="item.id" :label="item.realname" :value="item.id"/>
        </el-select>
      </el-form-item>

     <!-- <el-form-item
        label="负责人姓名:"
        prop="header">
        <el-input
          :maxlength="20"
          v-model="form.header"
          placeholder="请输入负责人姓名"
          clearable/>
      </el-form-item> -->

      <el-form-item label="显示顺序:" prop="sort">
        <el-input-number
          :min="0"
          v-model="form.sort"
          placeholder="请输入显示顺序"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>

      <el-form-item label="备注:">
        <el-input
          :rows="3"
          clearable
          type="textarea"
          :maxlength="200"
          v-model="form.note"
          placeholder="请输入备注"/>
      </el-form-item>
          </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>

export default {
  name: 'ClssEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: {},
  data() {
    return {
      // 表单数据
      form: Object.assign({status:1}, this.data),
      // 表单验证规则
      rules: {
              code: [
          {required: true, message: '请输入编号', trigger: 'blur'}
        ],
                  name: [
          {required: true, message: '请输入班级名称', trigger: 'blur'}
        ],
                  organization: [
          {required: true, message: '请输入所属组织', trigger: 'blur'}
        ],
        grade: [
          {required: true, message: '请输入所属年级', trigger: 'blur'}
        ],
                  superior: [
          {required: true, message: '请输入所属院系', trigger: 'blur'}
        ],
        stype: [
          {required: true, message: '请输入院系类型', trigger: 'blur'}
        ],

        professional: [
          {required: true, message: '请输入所属专业', trigger: 'blur'}
        ],

        status: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ],
        head: [
          {required: true, message: '请选择负责人', trigger: 'blur'}
        ],
        sort: [
          {required: true, message: '请输入显示顺序', trigger: 'blur'}
        ],
                  note: [
          {required: true, message: '请输入备注', trigger: 'blur'}
        ],
            },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      // 学院列表
      schoolList: [],
      // 父级院系列表
      departmentsList: [],
      // 年级列表
      gradeList: [],
      // 专业列表
      professionalList: [],
      // 负责人列表
      headList: []
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
        // 获取学院列表
        this.getSchoolList();
        // 获取父级院系列表
        this.getDepartmentsList();
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  mounted() {
    // 获取学院列表
    this.getSchoolList();
    // 获取父级院系列表
    this.getDepartmentsList();
    // 获取负责人列表
    this.getHeadlList();
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.form.features = JSON.stringify(this.form.features);
          // console.log(this.form.features);
          // return false;
          this.$http.post('/clss/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },
    /**
     * 获取组织列表
     */
    getSchoolList() {
      this.$http.get('/school/getSchoolList').then(res => {
        if (res.data.code === 0) {
          this.schoolList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },

    /**
     * 获取院系列表
     */
    getDepartmentsList() {
      if(this.form.organization){
        // this.form.superior = null;
        this.$forceUpdate();
        this.getGradeList();
        this.getProfessionalList();
      }
      this.$http.post('/departments/getDepartmentsList',{sid:this.form.organization,type:this.form.stype}).then(res => {
        if (res.data.code === 0) {
          this.departmentsList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /**
     * 获取年级列表
     */
    getGradeList() {
      if(this.form.organization){
        // this.form.grade = null;
        this.$forceUpdate();
      }
      this.$http.post('/grade/getGradeList',{oid:this.form.organization}).then(res => {
        if (res.data.code === 0) {
          this.gradeList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /**
     * 获取专业列表
     */
    getProfessionalList() {
      if(this.form.superior){
        // this.form.professional = null;
        this.$forceUpdate();
      }
      this.$http.post('/professional/getProfessionalList',{sid:this.form.superior}).then(res => {
        if (res.data.code === 0) {
          this.professionalList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /**
     * 获取负责人列表
     */
    getHeadlList() {
      this.$http.post('/user/getUserList',{type:1}).then(res => {
        if (res.data.code === 0) {
          this.headList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
  }
}
</script>

<style scoped>
</style>
